package process;

import gui.form.MainForm;
import gui.image.AlbumImageList;
import gui.image.FolderImageList;
import gui.image.ImageList;
import gui.treenode.AlbumTreeNode;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import org.ini4j.Profile;
import util.MyIniSection;

/**
 *
 * @author Quân
 */
public class ImportManager {

    private List<MyIniSection> albums = new ArrayList<>();
    public JPanel panel = null;

    public ImportManager(JPanel panel) {
        this.panel = panel;
    }

    public void Load() {
        try {
            for (String sectionName : qviewer.QViewer.iniFolder.keySet()) {
                Profile.Section section = qviewer.QViewer.iniFolder.get(sectionName);
                File folder = new File(section.get(MyIniSection.NodePath));
                ImageList imageList = new FolderImageList(folder, folder.getName());
                imageList.scan();

                MainForm mainForm = (MainForm) SwingUtilities.getRoot(panel);
                MutableTreeNode folderNode = mainForm.getFolderNode();
                MutableTreeNode node = new DefaultMutableTreeNode();
                node.setUserObject(new MyIniSection(section, imageList));
                folderNode.insert(node, folderNode.getChildCount());
                mainForm.jTreeReload();

                addOnGUI(imageList, section);
            }
            for (String sectionName : qviewer.QViewer.iniAlbum.keySet()) {
                Profile.Section section = qviewer.QViewer.iniAlbum.get(sectionName);
                File file = new File(section.get(MyIniSection.NodePath));
                AlbumImageList albumImageList = new AlbumImageList(file, section.get(MyIniSection.NodeName));
                albumImageList.scan();

                MainForm mainForm = (MainForm) SwingUtilities.getRoot(panel);
                MutableTreeNode albumNode = mainForm.getAlbumNode();
                MutableTreeNode node = new AlbumTreeNode(sectionName);
                MyIniSection myIniSection = new MyIniSection(section, albumImageList);
                node.setUserObject(myIniSection);
                albumNode.insert(node, albumNode.getChildCount());
                mainForm.jTreeReload();

                addOnGUI(albumImageList, section);
                albums.add(myIniSection);
            }
        } catch (Exception ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void Import(File folder) throws Exception {
        if (panel == null) {
            throw new Exception("A panel did not provider yet!");
        }

        ImageList imageList = new FolderImageList(folder, folder.getName());
        imageList.scan();
        UUID uniqueKey = UUID.randomUUID();
        Profile.Section section = qviewer.QViewer.iniFolder.add(uniqueKey.toString());
        section.add(MyIniSection.NodeName, folder.getName());
        section.add(MyIniSection.NodePath, folder.getAbsolutePath());
        try {
            qviewer.QViewer.iniFolder.store();
        } catch (IOException ex) {
            Logger.getLogger(ImageList.class.getName()).log(Level.SEVERE, null, ex);
        }

        MainForm mainForm = (MainForm) SwingUtilities.getRoot(panel);
        MutableTreeNode folderNode = mainForm.getFolderNode();
        MutableTreeNode node = new DefaultMutableTreeNode();
        node.setUserObject(new MyIniSection(section, imageList));
        folderNode.insert(node, folderNode.getChildCount());
        mainForm.jTreeReload();
        addOnGUI(imageList, section);
    }

    private void addOnGUI(ImageList imageList, Profile.Section section) {
        panel.add(imageList);
        Thread myThread = new Thread(imageList);
        myThread.start();
    }

    public List<MyIniSection> getAlbums() {
        return albums;
    }
}
